package com.fangmao.jetpack.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.SavedStateHandle;
import androidx.lifecycle.ViewModel;

/**
 * @ProjectName: wman
 * @Package: com.fangmao.view_model
 * @ClassName: HomeViewModel
 * @Description: java类作用描述
 * @Author: wman
 * @CreateDate: 2020/1/7 11:30
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/1/7 11:30
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class HomeViewModel extends ViewModel {
    /**
     * 保存状态的SavingStateLiveData
     */
    private SavedStateHandle mSavedStateHandle;
    private static final String KEY_NUMBER = "KEY_NUMBER";

    public HomeViewModel(SavedStateHandle handle) {
        this.mSavedStateHandle = handle;
    }

    public MutableLiveData<Integer> getNumber() {
        if (!mSavedStateHandle.contains(KEY_NUMBER)) {
            mSavedStateHandle.set(KEY_NUMBER, 0);
        }
        return mSavedStateHandle.getLiveData(KEY_NUMBER);
    }


    public void addNumber(int score) {
        getNumber().setValue(getNumber().getValue() + score);
        if (getNumber().getValue() < 0) {
            getNumber().setValue(0);
        }
    }
}
